package com.neusoft.controller;

import java.util.List;

import com.neusoft.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.DeliveryAddress;
import com.neusoft.service.DeliveryAddressService;

//@CrossOrigin("*")
@RefreshScope
@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {

	@Autowired
	private DeliveryAddressService deliveryAddressService;

	// 根据用户编号查询所属送货地址
	@GetMapping("/listDeliveryAddressByUserId/{userId}")
	public CommonResult<List> listDeliveryAddressByUserId(@PathVariable("userId") String userId) 
	throws Exception{
	System.out.println("Using DeliveryAddress_server_10500");
	System.out.println("userId: ");
	System.out.println(userId);
	 List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
	 return new CommonResult(200,"success",list);
	 }
	// 根据送货地址编号查询送货地址
	@GetMapping("/getDeliveryAddressById/{daId}")
	 public CommonResult<DeliveryAddress> getDeliveryAddressById(@PathVariable("daId") Integer
	daId) throws Exception{
	 System.out.println("Using DeliveryAddress_server_10500");
	 DeliveryAddress deliveryAddress = deliveryAddressService.getDeliveryAddressById(daId);
	 return new CommonResult(200,"success",deliveryAddress);
	 }

	// 向送货地址表中添加一条记录
	@PostMapping("/saveDeliveryAddress/{contactName}/{contactSex}/{contactTel}/{address}/{userId}")
	 public CommonResult<Integer> saveDeliveryAddress(
	 @PathVariable("contactName") String contactName,
	 @PathVariable("contactSex") Integer contactSex,
	 @PathVariable("contactTel") String contactTel,
	 @PathVariable("address") String address,
	 @PathVariable("userId") String userId) throws Exception{
	 System.out.println("Using DeliveryAddress_server_10500");
	 DeliveryAddress param = new DeliveryAddress();
	 param.setContactName(contactName);
	 param.setContactSex(contactSex);
	 param.setContactTel(contactTel);
	 param.setAddress(address);
	 param.setUserId(userId);
	 System.out.println(CommonUtil.getCurrentDate() + " 用户(" + param.getUserId() + ") 创建地址 " + param.getAddress());
	 int result = deliveryAddressService.saveDeliveryAddress(param);
	 return new CommonResult(200,"success",result);
	 }
	

	// 根据送货地址编号更新送货地址信息
	@PutMapping("/updateDeliveryAddress/{daId}/{contactName}/{contactSex}/{contactTel}/{address}")
	 public CommonResult<Integer> updateDeliveryAddress(
	 @PathVariable("daId") Integer daId,
	 @PathVariable("contactName") String contactName,
	 @PathVariable("contactSex") Integer contactSex,
	 @PathVariable("contactTel") String contactTel,
	 @PathVariable("address") String address) throws Exception{
	 System.out.println("Using DeliveryAddress_server_10500");
	 DeliveryAddress param = new DeliveryAddress();
	 param.setDaId(daId);
	 param.setContactName(contactName);
	 param.setContactSex(contactSex);
	 param.setContactTel(contactTel);
	 param.setAddress(address);
	 System.out.println(CommonUtil.getCurrentDate()+" 用户("+ param.getUserId() +") 修改地址 "+ param.getAddress() +"("+ param.getDaId()+")");
	 int result = deliveryAddressService.updateDeliveryAddress(param);
	 return new CommonResult(200,"success",result);
	 }

	// 根据送货地址编号删除一条记录
	@DeleteMapping("/removeDeliveryAddress/{daId}")
	 public CommonResult<Integer> removeDeliveryAddress(@PathVariable("daId") Integer daId) 
	throws Exception{
	 System.out.println("Using DeliveryAddress_server_10500");
	 int result = deliveryAddressService.removeDeliveryAddress(daId);
	 return new CommonResult(200,"success",result);
	 }
}
